// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.pages.forum;

import java.util.List;

import lichen.components.table.TablePages;
import lichen.entities.forum.Forum;
import lichen.entities.post.Topic;
import lichen.pages.Operations;
import lichen.pages.base.BasePage;
import lichen.pages.base.ITablePage;
import lichen.pages.base.LogInfoPage;
import lichen.pages.base.MenuPage;
import lichen.pages.topic.ViewTopic;
import lichen.services.Menu;
import lichen.services.PostService;
import lichen.services.UserService;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.corelib.components.Loop;
import org.apache.tapestry5.corelib.components.PageLink;
import org.apache.tapestry5.ioc.annotations.Inject;

/**
 * 浏览forum的页面
 * 
 * @author <a href="mailto:jun.tsai@gmail.com">Jun Tsai</a>
 * @version $Revision: 225 $
 * @since 0.0.1
 */
public class ViewForum extends BasePage implements LogInfoPage, MenuPage, ITablePage {

	// 当前的论坛对象
	private Forum forum;
	
	private int _nFirst = 0;
	
	private int _nPageSize;

	@Inject
	private UserService _userService;
	
	@Inject
	private PostService _postService;

	// 对论坛主题的循环操作.
	@SuppressWarnings("unused")
	@Component(parameters = { "source=topics", "value=topic" })
	private Loop topicLoop;

	// 对子论坛的循环操作.
	@SuppressWarnings("unused")
	@Component(parameters = { "source=childForums", "value=childForum" })
	private Loop childForumLoop;

	// 循环中的论坛主题的临时变量
	private Topic topic;

	// 循环中子论坛的临时变量
	private Forum childForum;

	private List childForums;

	// 跳转到论坛的组件
	@SuppressWarnings("unused")
	@Component(parameters = { "page=forum/view", "context=childForum" })
	private PageLink viewForum;

	// 新建主题的链接
//	@SuppressWarnings("unused")
//	@Component(parameters = { "page=topic/form", "context=newActionParameters" })
//	private PageLink newTopicLink;

	// 查看主题的链接
	@SuppressWarnings("unused")
	@Component(parameters = { "page=topic/view",
			"context=viewTopicActionParameters" })
	private PageLink viewTopicLink;

	// 当页面加载时候
	void onActivate(Forum forum) {
		this.forum = (Forum) forum;
		fetchChildForums();
	}

	// 当页面加载时候
	void onActivate(Forum forum, int nFirst, int nPageSize) {
		onActivate(forum);
		this._nFirst = nFirst;
		this._nPageSize = nPageSize;
	}

	
	// 固化的值
	Object[] onPassivate() {
		return new Object[]{forum,_nFirst, _nPageSize};
	}

	/**
	 * 得到当前的论坛对象
	 * 
	 * @return Returns the forum.
	 */
	public Forum getForum() {
		return forum;
	}

	public void setForum(Forum forum) {
		this.forum = forum;
	}

	/**
	 * 得到当前论坛的线程集合.
	 * 
	 * @return 当前论坛的线程集合.
	 */
	public List getTopics() {
		return _postService.getTopics(this.forum,this._nFirst,getNPageSize());
	}

	/**
	 * 得到子论坛的集合.
	 * 
	 * @return 得到子论坛的集合.
	 */
	public List getChildForums() {
		return this.childForums;
	}

	public boolean isHasChildForums() {
		return this.childForums.size() > 0;
	}

	public boolean isHasPosts() {
		return this.forum.getDepth() > 1;
	}

	private void fetchChildForums() {
		this.childForums = _postService.fetchChildForums(new Object[] {
				forum.getLeft(), forum.getRight(), forum.getDepth() + 1 });

	}

	/**
	 * @return Returns the childForum.
	 */
	public Forum getChildForum() {
		return childForum;
	}

	/**
	 * @param childForum
	 *            The childForum to set.
	 */
	public void setChildForum(Forum childForum) {
		this.childForum = childForum;
	}

	/**
	 * 删除论坛的主题.
	 * 
	 * @param topic
	 *            论坛主题.
	 */
	public void onActionFromDeleteTopic(Topic topic) {
		getEntityService().delete(topic);
	}

	/**
	 * 得到新增帖子的参数
	 * 
	 * @return 新建帖子时候的参数.
	 */
	public Object[] getNewActionParameters() {
		return new Object[] { Operations.NEW, this.forum};
	}

	/**
	 * 得到编辑帖子的参数
	 * 
	 * @return 编辑帖子时候的参数.
	 */
	public Object[] getEditActionParameters() {
		return new Object[] { Operations.EDIT, this.topic };
	}

	/**
	 * 查看帖子的参数
	 * 
	 * @return 查看帖子时候的参数.
	 */
	public Object[] getViewTopicActionParameters() {
		return new Object[] { this.topic, ViewTopic.FLAT_VIEW };
	}

	/**
	 * @return Returns the topic.
	 */
	public Topic getTopic() {
		return topic;
	}

	/**
	 * @param topic
	 *            The topic to set.
	 */
	public void setTopic(Topic topic) {
		this.topic = topic;
	}

	/**
	 * 
	 * @see lichen.pages.base.LogInfoPage#getLogInfo()
	 */
	public String getLogInfo() {
		return this.getMessages().format("forum.view", this.forum.getName());
	}

	public List<Menu> contributeMenus() {
		return this.getMenuService().generateForumMenu(this.forum);
	}

	/**
	 * 使用UserService中的isLogin方法来校验用户是否登录
	 * 
	 * @return true:登录 false:没有登录
	 * @since 0.0.3
	 */
	public boolean isLogin() {
		return _userService.isSystemUser();
	}
	
	/**
	 * 得到topic数量
	 * @return
	 * @since 0.0.3
	 */
	public long getTopicCount(){
		return _postService.getTopicCount(this.forum);
	}
	
	/**
	 * 翻页的链接
	 */
	public ViewForum onActionFromSplitPage(long pageNum){
		this._nFirst = ((int)((pageNum-1)*getNPageSize()));
		return this;
	}

	public int getNFirst() {
		return this._nFirst;
	}

	public void setNFirst(int nFirst) {
		this._nFirst = nFirst;
	}

	/**
	 * @return Returns the _nPageSize.
	 */
	public int getNPageSize() {
		if(_nPageSize<1){
			return tablePages.getNPageSize();
		} else{
			return _nPageSize;
		}
	}

	/**
	 * @param pageSize The _nPageSize to set.
	 */
	public void setNPageSize(int pageSize) {
		_nPageSize = pageSize;
	}
	
	@Component(parameters = { "totalRowCount=topicCount"})
	private TablePages tablePages;
}
